Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./year2.RDS")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.25, n = 505)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 11.36841 11.42483 11.48026 11.53471 11.58818 11.64068 11.69221 11.74279
##   [9] 11.79241 11.84109 11.88883 11.93564 11.98152 12.02648 12.07053 12.11368
##  [17] 12.15592 12.19727 12.23773 12.27731 12.31600 12.35378 12.39063 12.42655
##  [25] 12.46152 12.49553 12.52857 12.56063 12.59170 12.62176 12.65081 12.67883
##  [33] 12.70582 12.73175 12.75677 12.78100 12.80441 12.82696 12.84863 12.86939
##  [41] 12.88920 12.90804 12.92587 12.94267 12.95841 12.97305 12.98657 12.99893
##  [49] 13.01011 13.02008 13.02873 13.03605 13.04208 13.04687 13.05050 13.05301
##  [57] 13.05445 13.05490 13.05440 13.05300 13.05077 13.04777 13.04404 13.03965
##  [65] 13.03315 13.02327 13.01034 12.99468 12.97662 12.95649 12.93461 12.91131
##  [73] 12.88691 12.86174 12.83612 12.81038 12.78484 12.75984 12.73569 12.71272
##  [81] 12.69125 12.67163 12.65415 12.63612 12.61497 12.59125 12.56551 12.53830
##  [89] 12.51018 12.48169 12.45339 12.42583 12.39957 12.37515 12.35313 12.33406
##  [97] 12.31848 12.30473 12.29082 12.27688 12.26301 12.24934 12.23598 12.22304
## [105] 12.21065 12.19891 12.18795 12.17787 12.16880 12.16085 12.15414 12.14932
## [113] 12.14676 12.14616 12.14720 12.14957 12.15297 12.15708 12.16158 12.16618
## [121] 12.17055 12.17439 12.17739 12.17923 12.17961 12.17915 12.17871 12.17835
## [129] 12.17812 12.17807 12.17827 12.17876 12.17960 12.18084 12.18255 12.18476
## [137] 12.18753 12.19093 12.19500 12.19980 12.20538 12.21182 12.21912 12.22724
## [145] 12.23609 12.24563 12.25579 12.26651 12.27773 12.28938 12.30142 12.31378
## [153] 12.32639 12.33920 12.35214 12.36516 12.37819 12.39117 12.40404 12.41675
## [161] 12.43102 12.44841 12.46855 12.49106 12.51560 12.54179 12.56926 12.59767
## [169] 12.62664 12.65580 12.68480 12.71327 12.74084 12.76716 12.79185 12.81456
## [177] 12.83492 12.85256 12.86712 12.87824 12.88556 12.89172 12.89938 12.90822
## [185] 12.91787 12.92799 12.93824 12.94827 12.95773 12.96628 12.97358 12.97927
## [193] 12.98302 12.98447 12.98328 12.97910 12.97159 12.96013 12.94459 12.92534
## [201] 12.90274 12.87718 12.84902 12.81863 12.78640 12.75268 12.71786 12.68230
## [209] 12.64638 12.61048 12.57495 12.54018 12.50654 12.47439 12.44412 12.41609
## [217] 12.38541 12.34773 12.30422 12.25606 12.20444 12.15053 12.09550 12.04055
## [225] 11.98685 11.93557 11.88791 11.84503 11.80812 11.77835 11.75152 11.72293
## [233] 11.69305 11.66234 11.63127 11.60030 11.56990 11.54054 11.51267 11.48677
## [241] 11.46329 11.44271 11.42549 11.41210 11.40257 11.39638 11.39316 11.39252
## [249] 11.39407 11.39744 11.40225 11.40812 11.41466 11.42150 11.42824 11.43452
## [257] 11.43995 11.44415 11.44951 11.45839 11.47037 11.48504 11.50195 11.52070
## [265] 11.54086 11.56201 11.58373 11.60559 11.62717 11.64805 11.66781 11.68602
## [273] 11.70226 11.71611 11.72979 11.74562 11.76328 11.78244 11.80279 11.82402
## [281] 11.84581 11.86785 11.88981 11.91139 11.93227 11.95212 11.97064 11.98752
## [289] 12.00357 12.01984 12.03627 12.05284 12.06950 12.08622 12.10295 12.11967
## [297] 12.13633 12.15289 12.16931 12.18557 12.20161 12.21741 12.23292 12.24811
## [305] 12.26293 12.27735 12.29134 12.30494 12.31827 12.33133 12.34414 12.35673
## [313] 12.36911 12.38130 12.39331 12.40518 12.41691 12.42852 12.44003 12.45147
## [321] 12.46285 12.47418 12.48549 12.49715 12.50945 12.52230 12.53560 12.54927
## [329] 12.56320 12.57731 12.59149 12.60565 12.61971 12.63356 12.64711 12.66027
## [337] 12.67294 12.68502 12.69644 12.70708 12.71686 12.72567 12.73360 12.74079
## [345] 12.74732 12.75323 12.75859 12.76347 12.76791 12.77198 12.77573 12.77924
## [353] 12.78254 12.78572 12.78882 12.79191 12.79471 12.79695 12.79869 12.79998
## [361] 12.80086 12.80138 12.80160 12.80157 12.80133 12.80093 12.80044 12.79988
## [369] 12.79933 12.79882 12.79721 12.79354 12.78819 12.78150 12.77384 12.76555
## [377] 12.75701 12.74856 12.74056 12.73337 12.72736 12.72286 12.72026 12.71989
## [385] 12.72104 12.72270 12.72483 12.72734 12.73018 12.73329 12.73661 12.74006
## [393] 12.74359 12.74714 12.75065 12.75404 12.75726 12.76025 12.76294 12.76527
## [401] 12.76822 12.77265 12.77827 12.78481 12.79199 12.79954 12.80718 12.81465
## [409] 12.82165 12.82792 12.83318 12.83716 12.83959 12.84017 12.83984 12.83966
## [417] 12.83957 12.83954 12.83951 12.83943 12.83926 12.83894 12.83844 12.83769
## [425] 12.83666 12.83529 12.83354 12.83136 12.82839 12.82439 12.81946 12.81370
## [433] 12.80724 12.80017 12.79260 12.78465 12.77640 12.76798 12.75950 12.75105
## [441] 12.74274 12.73469 12.72700 12.71978 12.71313 12.70716 12.70199 12.69705
## [449] 12.69177 12.68622 12.68045 12.67453 12.66852 12.66249 12.65650 12.65060
## [457] 12.64488 12.63938 12.63418 12.62933 12.62490 12.62095 12.61754 12.61437
## [465] 12.61111 12.60780 12.60448 12.60118 12.59794 12.59481 12.59182 12.58901
## [473] 12.58641 12.58408 12.58204 12.58034 12.57901 12.57794 12.57700 12.57618
## [481] 12.57551 12.57498 12.57459 12.57436 12.57430 12.57439 12.57467 12.57512
## [489] 12.57575 12.57657 12.57759 12.57881 12.58022 12.58182 12.58361 12.58557
## [497] 12.58771 12.59002 12.59249 12.59512 12.59791 12.60084 12.60393 12.60715
## [505] 12.61051
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.25, n = 505)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 10.89673 10.97033 11.04273 11.11390 11.18384 11.25254 11.31998 11.38616
##   [9] 11.45105 11.51466 11.57696 11.63796 11.69763 11.75596 11.81294 11.86857
##  [17] 11.92283 11.97571 12.02719 12.07728 12.12595 12.17324 12.21917 12.26376
##  [25] 12.30705 12.34905 12.38979 12.42930 12.46760 12.50472 12.54067 12.57550
##  [33] 12.60922 12.64185 12.67310 12.70268 12.73068 12.75715 12.78216 12.80578
##  [41] 12.82808 12.84912 12.86898 12.88773 12.90542 12.92213 12.93792 12.95287
##  [49] 12.96704 12.98050 12.99219 13.00118 13.00775 13.01218 13.01475 13.01575
##  [57] 13.01545 13.01414 13.01208 13.00957 13.00689 13.00431 13.00211 13.00058
##  [65] 12.99883 12.99585 12.99171 12.98648 12.98021 12.97299 12.96486 12.95591
##  [73] 12.94620 12.93580 12.92476 12.91317 12.90109 12.88857 12.87571 12.86255
##  [81] 12.84916 12.83562 12.82199 12.80681 12.78888 12.76859 12.74635 12.72256
##  [89] 12.69764 12.67198 12.64599 12.62008 12.59465 12.57011 12.54687 12.52532
##  [97] 12.50589 12.48526 12.46039 12.43205 12.40102 12.36806 12.33394 12.29944
## [105] 12.26532 12.23237 12.20134 12.17301 12.14816 12.12755 12.11195 12.09854
## [113] 12.08417 12.06913 12.05370 12.03818 12.02286 12.00803 11.99399 11.98101
## [121] 11.96940 11.95945 11.95144 11.94566 11.94242 11.94122 11.94137 11.94279
## [129] 11.94543 11.94921 11.95408 11.95997 11.96682 11.97456 11.98313 11.99247
## [137] 12.00251 12.01319 12.02444 12.03620 12.04841 12.06285 12.08107 12.10269
## [145] 12.12729 12.15449 12.18389 12.21510 12.24772 12.28135 12.31561 12.35008
## [153] 12.38438 12.41812 12.45089 12.48230 12.51196 12.53946 12.56442 12.58644
## [161] 12.60809 12.63201 12.65791 12.68549 12.71445 12.74450 12.77534 12.80667
## [169] 12.83821 12.86964 12.90068 12.93103 12.96040 12.98848 13.01498 13.03961
## [177] 13.06206 13.08205 13.09927 13.11343 13.12423 13.13387 13.14453 13.15593
## [185] 13.16775 13.17970 13.19149 13.20282 13.21340 13.22291 13.23108 13.23759
## [193] 13.24216 13.24448 13.24426 13.24120 13.23500 13.22540 13.21251 13.19659
## [201] 13.17792 13.15678 13.13342 13.10813 13.08117 13.05281 13.02333 12.99300
## [209] 12.96208 12.93085 12.89958 12.86854 12.83800 12.80824 12.77952 12.75211
## [217] 12.72222 12.68649 12.64585 12.60124 12.55359 12.50383 12.45290 12.40173
## [225] 12.35126 12.30241 12.25613 12.21334 12.17498 12.14198 12.10981 12.07378
## [233] 12.03464 11.99313 11.94998 11.90593 11.86172 11.81808 11.77575 11.73547
## [241] 11.69798 11.66401 11.63430 11.60959 11.58784 11.56662 11.54604 11.52622
## [249] 11.50725 11.48927 11.47238 11.45670 11.44233 11.42941 11.41802 11.40830
## [257] 11.40036 11.39430 11.39097 11.39089 11.39375 11.39922 11.40698 11.41670
## [265] 11.42806 11.44073 11.45439 11.46872 11.48338 11.49806 11.51242 11.52616
## [273] 11.53893 11.55042 11.56293 11.57867 11.59721 11.61809 11.64088 11.66511
## [281] 11.69035 11.71615 11.74207 11.76765 11.79245 11.81602 11.83792 11.85770
## [289] 11.87740 11.89920 11.92287 11.94814 11.97479 12.00256 12.03122 12.06051
## [297] 12.09020 12.12004 12.14978 12.17918 12.20800 12.23599 12.26291 12.28852
## [305] 12.31256 12.33480 12.35500 12.37394 12.39260 12.41097 12.42905 12.44683
## [313] 12.46432 12.48152 12.49841 12.51500 12.53129 12.54728 12.56295 12.57832
## [321] 12.59338 12.60812 12.62254 12.63658 12.65018 12.66337 12.67617 12.68861
## [329] 12.70070 12.71248 12.72396 12.73518 12.74615 12.75690 12.76745 12.77783
## [337] 12.78806 12.79816 12.80817 12.81809 12.82797 12.83782 12.84721 12.85577
## [345] 12.86361 12.87081 12.87748 12.88371 12.88961 12.89528 12.90080 12.90629
## [353] 12.91183 12.91754 12.92350 12.92981 12.93627 12.94258 12.94876 12.95481
## [361] 12.96073 12.96654 12.97224 12.97782 12.98331 12.98870 12.99401 12.99922
## [369] 13.00436 13.00943 13.01431 13.01889 13.02321 13.02732 13.03124 13.03503
## [377] 13.03871 13.04233 13.04592 13.04952 13.05317 13.05691 13.06077 13.06480
## [385] 13.06929 13.07442 13.08007 13.08612 13.09245 13.09894 13.10547 13.11192
## [393] 13.11816 13.12408 13.12955 13.13445 13.13866 13.14207 13.14454 13.14596
## [401] 13.14710 13.14868 13.15059 13.15267 13.15479 13.15680 13.15858 13.15997
## [409] 13.16084 13.16105 13.16046 13.15893 13.15632 13.15249 13.14721 13.14048
## [417] 13.13250 13.12348 13.11361 13.10311 13.09217 13.08100 13.06980 13.05878
## [425] 13.04813 13.03806 13.02877 13.02047 13.01176 13.00125 12.98917 12.97572
## [433] 12.96114 12.94563 12.92941 12.91270 12.89572 12.87868 12.86181 12.84532
## [441] 12.82943 12.81435 12.80031 12.78752 12.77619 12.76656 12.75883 12.75234
## [449] 12.74631 12.74069 12.73548 12.73066 12.72621 12.72211 12.71835 12.71489
## [457] 12.71174 12.70886 12.70624 12.70386 12.70170 12.69975 12.69798 12.69644
## [465] 12.69516 12.69416 12.69344 12.69300 12.69287 12.69304 12.69352 12.69432
## [473] 12.69545 12.69691 12.69871 12.70085 12.70335 12.70619 12.70934 12.71281
## [481] 12.71659 12.72069 12.72510 12.72983 12.73487 12.74022 12.74588 12.75186
## [489] 12.75814 12.76474 12.77165 12.77885 12.78633 12.79409 12.80214 12.81047
## [497] 12.81909 12.82801 12.83721 12.84672 12.85652 12.86662 12.87703 12.88774
## [505] 12.89876
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.25, n = 505)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 10.87589 10.93408 10.99132 11.04759 11.10290 11.15724 11.21061 11.26301
##   [9] 11.31444 11.36489 11.41436 11.46285 11.51035 11.55686 11.60239 11.64692
##  [17] 11.69046 11.73301 11.77455 11.81509 11.85464 11.89321 11.93081 11.96744
##  [25] 12.00309 12.03776 12.07146 12.10419 12.13594 12.16671 12.19651 12.22533
##  [33] 12.25318 12.28005 12.30583 12.33040 12.35381 12.37609 12.39728 12.41741
##  [41] 12.43650 12.45461 12.47176 12.48799 12.50332 12.51781 12.53147 12.54434
##  [49] 12.55647 12.56788 12.57828 12.58742 12.59536 12.60218 12.60794 12.61272
##  [57] 12.61658 12.61960 12.62185 12.62339 12.62430 12.62465 12.62450 12.62393
##  [65] 12.62188 12.61739 12.61068 12.60198 12.59151 12.57949 12.56616 12.55173
##  [73] 12.53643 12.52048 12.50411 12.48754 12.47100 12.45471 12.43890 12.42378
##  [81] 12.40959 12.39655 12.38488 12.37295 12.35918 12.34386 12.32730 12.30981
##  [89] 12.29168 12.27321 12.25472 12.23649 12.21884 12.20206 12.18647 12.17235
##  [97] 12.16001 12.14827 12.13584 12.12287 12.10951 12.09591 12.08222 12.06858
## [105] 12.05515 12.04207 12.02950 12.01758 12.00647 11.99631 11.98726 11.97913
## [113] 11.97162 11.96466 11.95819 11.95216 11.94649 11.94114 11.93603 11.93110
## [121] 11.92630 11.92156 11.91682 11.91202 11.90710 11.90155 11.89503 11.88776
## [129] 11.87994 11.87178 11.86347 11.85522 11.84724 11.83973 11.83290 11.82695
## [137] 11.82209 11.81851 11.81643 11.81604 11.81756 11.82022 11.82313 11.82634
## [145] 11.82988 11.83379 11.83810 11.84287 11.84812 11.85389 11.86022 11.86715
## [153] 11.87472 11.88296 11.89192 11.90162 11.91212 11.92344 11.93563 11.94873
## [161] 11.96442 11.98412 12.00740 12.03384 12.06303 12.09455 12.12797 12.16288
## [169] 12.19886 12.23549 12.27236 12.30904 12.34511 12.38017 12.41378 12.44553
## [177] 12.47500 12.50178 12.52544 12.54557 12.56174 12.57759 12.59664 12.61829
## [185] 12.64196 12.66708 12.69306 12.71931 12.74525 12.77030 12.79387 12.81538
## [193] 12.83425 12.84988 12.86171 12.86914 12.87158 12.86914 12.86253 12.85213
## [201] 12.83832 12.82147 12.80196 12.78017 12.75646 12.73122 12.70482 12.67764
## [209] 12.65005 12.62243 12.59515 12.56860 12.54313 12.51914 12.49700 12.47708
## [217] 12.45523 12.42768 12.39535 12.35917 12.32007 12.27897 12.23680 12.19448
## [225] 12.15296 12.11314 12.07596 12.04234 12.01322 11.98951 11.96807 11.94531
## [233] 11.92153 11.89706 11.87218 11.84720 11.82244 11.79819 11.77476 11.75246
## [241] 11.73159 11.71245 11.69536 11.68061 11.66765 11.65572 11.64478 11.63479
## [249] 11.62572 11.61754 11.61022 11.60371 11.59799 11.59303 11.58878 11.58522
## [257] 11.58231 11.58002 11.57942 11.58141 11.58572 11.59205 11.60011 11.60962
## [265] 11.62030 11.63185 11.64399 11.65644 11.66890 11.68109 11.69273 11.70353
## [273] 11.71320 11.72145 11.72958 11.73894 11.74936 11.76067 11.77269 11.78527
## [281] 11.79821 11.81136 11.82454 11.83757 11.85030 11.86253 11.87411 11.88486
## [289] 11.89568 11.90753 11.92029 11.93385 11.94809 11.96291 11.97819 11.99382
## [297] 12.00968 12.02567 12.04167 12.05757 12.07326 12.08863 12.10355 12.11793
## [305] 12.13165 12.14459 12.15664 12.16889 12.18235 12.19683 12.21210 12.22797
## [313] 12.24423 12.26067 12.27708 12.29327 12.30901 12.32412 12.33837 12.35156
## [321] 12.36349 12.37396 12.38274 12.39001 12.39613 12.40121 12.40534 12.40861
## [329] 12.41114 12.41303 12.41436 12.41524 12.41578 12.41606 12.41620 12.41628
## [337] 12.41642 12.41671 12.41724 12.41813 12.41946 12.42134 12.42322 12.42451
## [345] 12.42531 12.42570 12.42575 12.42555 12.42518 12.42472 12.42425 12.42387
## [353] 12.42364 12.42365 12.42398 12.42472 12.42487 12.42357 12.42105 12.41755
## [361] 12.41331 12.40858 12.40360 12.39860 12.39383 12.38954 12.38596 12.38333
## [369] 12.38189 12.38189 12.38277 12.38382 12.38504 12.38644 12.38802 12.38979
## [377] 12.39175 12.39392 12.39629 12.39888 12.40168 12.40470 12.40796 12.41145
## [385] 12.41596 12.42214 12.42974 12.43851 12.44823 12.45864 12.46951 12.48059
## [393] 12.49166 12.50246 12.51275 12.52231 12.53088 12.53822 12.54410 12.54827
## [401] 12.55195 12.55639 12.56142 12.56688 12.57259 12.57839 12.58410 12.58956
## [409] 12.59461 12.59906 12.60276 12.60553 12.60721 12.60762 12.60677 12.60486
## [417] 12.60203 12.59841 12.59414 12.58936 12.58421 12.57882 12.57334 12.56789
## [425] 12.56262 12.55766 12.55315 12.54924 12.54491 12.53919 12.53223 12.52421
## [433] 12.51528 12.50560 12.49533 12.48465 12.47370 12.46265 12.45166 12.44090
## [441] 12.43053 12.42070 12.41158 12.40334 12.39613 12.39011 12.38545 12.38166
## [449] 12.37814 12.37488 12.37186 12.36906 12.36648 12.36409 12.36188 12.35984
## [457] 12.35796 12.35621 12.35459 12.35308 12.35166 12.35032 12.34904 12.34786
## [465] 12.34681 12.34589 12.34513 12.34452 12.34407 12.34379 12.34369 12.34378
## [473] 12.34407 12.34455 12.34525 12.34616 12.34730 12.34866 12.35020 12.35193
## [481] 12.35386 12.35596 12.35825 12.36072 12.36338 12.36620 12.36921 12.37239
## [489] 12.37573 12.37925 12.38294 12.38678 12.39078 12.39493 12.39923 12.40370
## [497] 12.40832 12.41310 12.41805 12.42316 12.42844 12.43389 12.43951 12.44530
## [505] 12.45127
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")

keeping in case

#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")